package com.intct.flink.study;

import com.intct.func.TestFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.configuration.RestOptions;
import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

/**
 * @author gufg
 * @since 2025-10-13 15:25
 */
public class SQLTest06 {
    public static void main(String[] args) {
        // 配置属性
        Configuration conf = new Configuration();
        conf.set(RestOptions.BIND_PORT, "8081");

        // 1 获取取环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        // 并行度
        // env.setParallelism(1);
        // 开启检点
        env.enableCheckpointing(5000L, CheckpointingMode.EXACTLY_ONCE);
        // 获取SQL执行环境
        StreamTableEnvironment tenv = StreamTableEnvironment.create(env);

        // 注册函数
        tenv.createTemporarySystemFunction("test_fun", TestFunction.class);

        tenv.executeSql(
                "CREATE TABLE orders (" +
                        "    order_id STRING," +
                        "    product_id STRING," +
                        "    amount DECIMAL(10, 2)," +
                        "    event_time TIMESTAMP(3)," +
                        "    WATERMARK FOR event_time AS event_time -INTERVAL'5'SECOND" +
                        ") WITH (" +
                        "    'connector'='kafka'," +
                        "    'topic'='orders'," +
                        "    'properties.bootstrap.servers'='cdn-node:9092'," +
                        "    'properties.group.id'='lookup_join_group'," +
                        "    'format'='json'," +
                        "    'scan.startup.mode'='latest-offset'" +
                        ");");
        
        tenv.executeSql(
                "CREATE TABLE products (" +
                        "    product_id STRING," +
                        "    product_name STRING," +
                        "    price DECIMAL(10, 2)," +
                        "    PRIMARY KEY (product_id) NOT ENFORCED  -- 主键（用于查询）" +
                        ") WITH (" +
                        "    'connector'='jdbc'," +
                        "    'url'='jdbc:mysql://cdh-node:3306/m1'," +
                        "    'username'='root'," +
                        "    'password'='Test_090110'," +
                        "    'table-name'='customers'," +
                        "    'lookup.cache.max-rows'='10000',  -- 缓存最大行数" +
                        "    'lookup.cache.ttl'='1h',          -- 缓存 TTL（1 小时）" +
                        "    'lookup.max-retries'='3'           -- 查询失败重试次数" +
                        ")");

        tenv.executeSql(
                "CREATE TABLE order_product_result (" +
                        "    order_id STRING," +
                        "    product_name STRING," +
                        "    amount DECIMAL(10, 2)" +
                        ") WITH (" +
                        "    'connector'='print'" +
                        ")");
        
        tenv.executeSql("INSERT INTO order_product_result" +
                "SELECT" +
                "    o.order_id, " +
                "    p.product_name, " +
                "    o.amount" +
                "FROM orders AS o" +
                "JOIN products FORSYSTEM_TIMEASOF o.event_time AS p" +
                "ON o.product_id = p.product_id;");
    }
}
